﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Single_SignOn.Areas.PDA.Models;
using Single_SignOn.Attributes;
using Single_SignOn.Models;
using Single_SignOn.ResX;

namespace Single_SignOn.Areas.PDA.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /IE5/Login/

        public ActionResult Index()
        {
            Login models = new Login();

            if (Request.QueryString["Username"] != null)
                models.Username = Request.QueryString["Username"];

            return View("Index", models);
        }


        [HttpPost]
        [MultipleButton(Name = "State", Argument = "Login")]
        public ActionResult Login(Login models)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DataAccess.SingleSignOn.AuthenConnection oAuth = new DataAccess.SingleSignOn.AuthenConnection();
                    DataTable dt =
                        // Called Data Access
                       oAuth.pullUserProfile(models.Username, th.co.homepro.Utility.zEncrypt(models.Password, WebConfig.Encryptkey, WebConfig.EncryptValue));
                    
                    if (dt != null)
                        if (dt.Rows.Count > 0)
                        {

                            DataTable dtUserData = oAuth.pullUserProfile(models.Username);
                            UserProfile oUserProfile;
                            if (dtUserData != null)
                                if (dtUserData.Rows.Count > 0)
                                {
                                    Session.Remove("UserInfo");
                                    oUserProfile = new UserProfile();
                                    oUserProfile.Description = (System.DBNull.Value == dtUserData.Rows[0]["DESCRIPTION"]) ? "" : (string)dtUserData.Rows[0]["DESCRIPTION"];
                                    oUserProfile.GroupDescription = (System.DBNull.Value == dtUserData.Rows[0]["GROUPDESC"]) ? "" : (string)dtUserData.Rows[0]["GROUPDESC"];
                                    oUserProfile.EmployeeNo = (System.DBNull.Value == dtUserData.Rows[0]["EMPID"]) ? "" : (string)dtUserData.Rows[0]["EMPID"];
                                    oUserProfile.Username = (System.DBNull.Value == dtUserData.Rows[0]["EMPNO"]) ? "" : (string)dtUserData.Rows[0]["EMPNO"];
                                    oUserProfile.FullName = (System.DBNull.Value == dtUserData.Rows[0]["EMPNAME"]) ? "" : (string)dtUserData.Rows[0]["EMPNAME"];
                                    oUserProfile.Password = (System.DBNull.Value == dtUserData.Rows[0]["PASSWORD"]) ? "" : (string)dtUserData.Rows[0]["PASSWORD"];
                                    oUserProfile.GroupNo = (System.DBNull.Value == dtUserData.Rows[0]["GROUPNO"]) ? "" : (string)dtUserData.Rows[0]["GROUPNO"];
                                    oUserProfile.Division = (System.DBNull.Value == dtUserData.Rows[0]["DIVISION"]) ? "" : (string)dtUserData.Rows[0]["DIVISION"];
                                    oUserProfile.Store = (System.DBNull.Value == dtUserData.Rows[0]["STORENO"]) ? "" : (string)dtUserData.Rows[0]["STORENO"];
                                    oUserProfile.Tel = (System.DBNull.Value == dtUserData.Rows[0]["TEL"]) ? "" : (string)dtUserData.Rows[0]["TEL"];
                                    oUserProfile.Email = (System.DBNull.Value == dtUserData.Rows[0]["EMAIL"]) ? "" : (string)dtUserData.Rows[0]["EMAIL"];
                                    oUserProfile.Status = (System.DBNull.Value == dtUserData.Rows[0]["STATUS"]) ? "" : (string)dtUserData.Rows[0]["STATUS"];
                                    oUserProfile.ApplNo = (System.DBNull.Value == dtUserData.Rows[0]["APPLNO"]) ? "" : (string)dtUserData.Rows[0]["APPLNO"];
                                    oUserProfile.ProfileLevel = (System.DBNull.Value == dtUserData.Rows[0]["PROFILELEVEL"]) ? "" : (string)dtUserData.Rows[0]["PROFILELEVEL"];
                                    oUserProfile.Language = (System.DBNull.Value == dtUserData.Rows[0]["LANG"]) ? "" : (string)dtUserData.Rows[0]["LANG"];
                                    Session["UserInfo"] = oUserProfile;
                                }
                            //*
                            UserProfile o = new UserProfile();
                            return RedirectToAction("Index","ApplicationList");
                        }
                        else
                        {
                            ModelState.AddModelError("DataNotFound", ResourceTools.GetResource("Resource", "DataNotFound"));
                        }

                    else
                    {
                        ModelState.AddModelError("DataNotFound", ResourceTools.GetResource("Resource", "DataNotFound"));
                    }

                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("MessageException", ex.Message);
                }
            }

            return View("Index");
        }


        [HttpPost]
        [MultipleButton(Name = "State", Argument = "ChangePassword")]
        public ActionResult ChangePassword(Login models)
        {
            return RedirectToAction("Index", "ChangeSword",new {Username= models.Username});
        }
        
        [AllowAnonymous]
        public ActionResult LogOut()
        {
            Session.Remove("UserInfo");
            Session["UserInfo"] = null;
            return RedirectToAction("Index", "Login");
        }

    }
}
